package logic.facade;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import logic.service.SubjectService;
import vo.IValueObject;
import vo.SubjectVO;

/**
 *
 * @author Alexander
 */
public class SubjectFacade extends Facade {

    public SubjectFacade() {
        setService(new SubjectService());
    }

    @Override
    protected boolean exist(IValueObject vo) {
        return !(super.find(((SubjectVO) vo).getCode()) == null);
    }
    
//    public List<String> SubjectNames(){
//     EntityManagerFactory emf = null;
//        EntityManager em = null;
//        EntityTransaction tx = null;
//        List<String> subName = new ArrayList<>();
//        try {
//            emf = Persistence.createEntityManagerFactory("StunnicusPU");
//            em = emf.createEntityManager();
//            subName = ((SubjectService) getService())
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (em != null) {
//                em.clear();
//                em.close();
//            }
//            return subName;
//        }
//    }

    public List<SubjectVO> search(Object guess, boolean byCode) {
        EntityManagerFactory emf = null;
        EntityManager em = null;
        EntityTransaction tx = null;
        List<SubjectVO> materias = new ArrayList<>();
        try {
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            materias = ((SubjectService) getService()).search(guess, byCode, em);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null) {
                em.clear();
                em.close();
            }
            return materias;
        }
    }

     public List<SubjectVO> getSubjectsByUser(String user) {
        EntityManagerFactory emf = null;
        EntityManager em = null;
        EntityTransaction tx = null;
        List<SubjectVO> materias = new ArrayList<>();
        try {
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            materias = ((SubjectService) getService()).getSubjectsByUser(user, em);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null) {
                em.clear();
                em.close();
            }
            return materias;
        }
    }

    public List<SubjectVO> getSubjects() {
        EntityManagerFactory emf = null;
        EntityManager em = null;
        EntityTransaction tx = null;
        List<SubjectVO> materias = new ArrayList<>();
        try {
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            materias = ((SubjectService) getService()).getSubjects(em);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null) {
                em.clear();
                em.close();
            }
            return materias;
        }
    }
}
